#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1e5+10;
const int MAXB = 340;

deque <int> q[MAXB];

int BL,n,lasans,cnt[MAXB][MAXN],bl[MAXN],st[MAXB],en[MAXB];

int main (){
	#ifndef ONLINE_JUDGE
	freopen("1471_8_in.txt","r",stdin);
	freopen("1471_8.out","w",stdout);
	#endif
	cin>>n; BL = sqrt(n);
	for(int i = 1;i <= n;i++) {
		bl[i] = (i - 1) / BL + 1;
		if(!st[bl[i]]) st[bl[i]] = i;
		en[bl[i]] = i;
		int x; cin>>x;
		cnt[bl[i]][x]++;
		q[bl[i]].push_back(x);
	}
	int Q;
	cin>>Q;
	while(Q--){
		int op,l,r,x,y,k;
		cin >> op>>l>>r;
		l = (l+lasans-1)%n+1; r = (r+lasans-1)%n+1;
		if(l > r) swap(l,r);
		r++;
		x = bl[l];  y = bl[r];
		if(op == 1){
			if(x == y){
				l = l - st[x]; 	r = r - 1- st[y];
				int num = q[x][r];
				q[x].erase(q[x].begin()+r);
				q[x].insert(q[x].begin()+l,num);
			}else{
				for(int i = x;i < y;i++){
					int num = q[i].back();
					cnt[i][num]--;
					q[i].pop_back();
					q[i+1].push_front(num);
					cnt[i+1][num]++;
				}
				int num = q[y][r-st[y]];
				q[y].erase(q[y].begin()+r-st[y]); cnt[y][num]--;
				q[x].insert(q[x].begin()+l-st[x],num);
				cnt[x][num]++;
			}
		}else{
			cin>>k;
			k = (k+lasans-1)%n+1;
			if(x == y){
				lasans = 0;
				l = l - st[x] ;r = r - st[y];
				for(int i = l;i < r;i++) lasans += q[x][i] == k;
			}else{
				lasans = 0;
				for(int i = x+1;i < y;i++) lasans += cnt[i][k];
				l = l - st[x];r = r-st[y];
				for(int i = l;i < (int)q[x].size();i++) lasans += q[x][i] == k;
				for(int i = 0;i < r;i++) lasans += q[y][i] == k;
			}
			cout << lasans<<endl;
		}
	}
	return 0;
}
